<?php
/**
 * Created by PhpStorm.
 * User: Frings
 * Date: 2016-04-26
 * Time: 11:03
 */

use \yii\helpers\Url;

$web = Url::to('@web');
?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta content="telephone=no" name="format-detection">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
    <meta name="apple-mobile-web-app-capable" content="no">
    <title>订单详情</title>
    <link type="text/css" href="<?= $web ?>/css/wx-good/global.css" rel="stylesheet"/>
    <link type="text/css" href="<?= $web ?>/css/wx-good/order-detail.css" rel="stylesheet"/>

    <script type="text/javascript" src="<?= $web ?>/js/jquery.js"></script>
    <script type="text/javascript" src="<?= $web ?>/js/tools.js"></script>
    <?php require_once Yii::$app->viewPath . '/wx-no-share.php' ?>
    <script type="application/javascript">
        $(document).ready(function () {
            var w = $(document).width();
            if (w > 640) {
                w = 640;
            }
            $("html").css("font-size", w * 20 / 320);
        });

        function callpay() {
//            var str=prompt("暂时没有开通此功能");
//            if(str)
//            {
//               if(str!='321'){
//                   return;
//               }
//            }else{
//                return;
//            }

            $("#btnCallPay").unbind();

            $("#pay_confirm_dialog").hide();
            $("#loading_toast").show();
            var order_code = "<?= $order['CODE'] ?>";
            var current_coin = "<?= $order['COIN'] ?>";
            var data = {};
            data.order_id = "<?= $order['ID'] ?>";
            requestJson("<?=Url::to(["wx-good/wx-pay-order-handle"])?>", data, true, function (response) {
                $("#loading_toast").hide();
                if (response.status == 0) {
                    //微信支付
                    jsApiCall(response.data.jsApiParameters);
                    //苹果手机不能执行这句，否则不能支付
                   // window.location.reload();

                } else if (response.status == 1111) {//测试用
                    cashCoin(response.data.testcode, response.data.coin, response.data.price);
                    alert('支付成功！订单编号：' + response.data.testcode + ";现金支付:" + response.data.price + "分");
                    window.location.reload();
                } else if (response.status == 100) {//0分支付
                    cashCoin(order_code, current_coin, 0);
                    alert('支付成功！');
                    window.location.reload();
                } else if (response.status == 10) {
                    alert('未找到此订单！');
                    window.location.reload();
                } else if (response.status == -1) {
                    alert('当前用户朵拉币不足！');
                    window.location.reload();
                } else if (response.status == 120) {
                    alert(response.message);
                    window.location.reload();
                } else {
                    alert(response.message);
                }

                $("#btnCallPay").click(function () {
                    callpay();
                });
            });

        }
        //扣除朵拉币
        function cashCoin(order_code, current_coin, realPrice) {
            var data = {};
            data.order_code = order_code;
            data.current_coin = current_coin;
            data.realPrice = realPrice;
            var re = returnjson("<?=Url::to(["wx-home/cash-dora-coin-handle"])?>", data);
            if (re.flag == 1) {
                return true;
            } else {
                alert(re.msg);
                return false;
            }
        }
        //调用微信JS api 支付
        function jsApiCall(jsApiParameters) {
            var order_code = "<?= $order['CODE'] ?>";
            var current_coin = "<?= $order['COIN'] ?>";
            WeixinJSBridge.invoke(
                'getBrandWCPayRequest',
                jsApiParameters,
                function (res) {
                    if (res.err_msg == "get_brand_wcpay_request:ok") {
                        var r = cashCoin(order_code, current_coin, 0);
                        if (r) {
                            alert('支付成功！');
                            window.location.reload();
                        } else
                            alert('支付失败！');


                    } else {
                        alert('支付取消！');
                    }
                }
            );
        }

        function payCancel() {
            $("#pay_confirm_dialog").hide();
        }

        function payConfirm() {
            $("#pay_confirm_dialog").show();
        }

        function orderCancel() {
            $("#order_cancel_confirm_dialog").show();
        }

        function orderCancelCancel() {
            $("#order_cancel_confirm_dialog").hide();
        }

        function orderCancelOk() {
            $("#order_cancel_confirm_dialog").hide();
            cancelOrder();
        }

        function cancelOrder() {
            $("#loading_toast").show();
            var data = {};
            data.order_id = "<?= $order['ID'] ?>";
            requestJson("<?=Url::to(["wx-good/cancel-order-handle"])?>", data, true, function (response) {
                $("#loading_toast").hide();
                if (response.status == 0) {
                    $("#toast").show();
                    window.location.href = '<?=Url::to(["wx-good/order-list"])?>';
                } else {
                    alert(response.message);
                }
            });
        }

        function searchOrderLogistics() {
            var order_id = "<?= $order['ID'] ?>";
            window.location.href = '<?=Url::to(["wx-good/order-logistics"])?>' + '?order_id=' + order_id;
        }
    </script>
</head>
<body>
<div class="main">
    <div class="order-info-frame">
        <img class="datetime-icon" src="<?= $web ?>/img/wx-good/order-list-datetime-icon.png">
        <label class="datetime-text"><?= $order['CREATE_DATETIME'] ?></label>
        <label class="code-text"><?= $order['CODE'] ?></label>
        <label class="status-text"><?= $order['status_text'] ?></label>
    </div>
    <div class="head-head-top"></div>
    <div class="head">
        <div class="head-border">
            <img src="<?= $web ?>/img/wx-good/order-1.png" style="width: 100%">
        </div>
        <div class="head-user">
            <?php
            if ($order['TYPE'] != 2) {
                ?>
                <div class="user-people">

                    <div class="user-people-img">
                        <img src="<?= $web ?>/img/wx-good/order-3.png" style="height: 100%">
                    </div>
                    <div class="user-people-name">
                        <span><?= $order['RECEIVER_NAME'] ?></span>
                    </div>

                </div>
                <?php
            }
            ?>
            <div class="user-phone">
                <div class="user-phone-img">
                    <img src="<?= $web ?>/img/wx-good/order-4.png" style="height: 100%">
                </div>
                <div class="user-phone-number">
                    <span><?= $order['RECEIVER_PHONE'] ?></span>
                </div>
            </div>
        </div>
        <?php
        if ($order['TYPE'] != 2) {
            ?>
            <div class="user-address">
                <span><?= $order['RECEIVER_PROVINCE'] . $order['RECEIVER_CITY'] . $order['RECEIVER_REGION'] . $order['RECEIVER_ADDRESS'] ?></span>
            </div>
            <?php
        }
        ?>


    </div>
    <div class="commodity-frame">
        <?php
        if (isset($goodsList)) {
            foreach ($goodsList as $goods) {
                $imageUrl = isset($goods['GOODS_IMG_URL']) ? $goods['GOODS_IMG_URL'] : "$web/img/wx-good/goods-default.jpg";
                ?>
                <div class="commodity">
                    <div class="commodity-img">
                        <img src="<?= $imageUrl ?>" style="width: 3rem; height: 3rem;">
                    </div>
                    <div class="commodity-details">
                        <div class="commodity-name">
                            <?= $goods['GOODS_NAME'] ?>
                        </div>
                        <div class="commodity-integral">
                            金额&nbsp;&nbsp;&nbsp;&nbsp;<span><?= sprintf('%.2f', $goods['PRICE']) ?></span>
                        </div>
                        <div class="commodity-number">
                            数量&nbsp;&nbsp;&nbsp;&nbsp;<span class="span-x">x</span><span
                                class="span-number"><?= $goods['COUNT'] ?></span>
                        </div>
                    </div>
                </div>
                <?php
            }
        }
        ?>
    </div>
    <div class="price">
        <div class="price-div price-top">
            <div class="price-left">运费</div>
            <div class="price-right">0.00 元</div>
        </div>
        <div class="price-div price-bottom">
            <div class="price-left">总价</div>
            <div class="price-right"><?= sprintf('%.2f', $order['PRICE']) ?> 元</div>
        </div>
    </div>
    <?php
    if ($order['STATUS'] == 1) {  // 为支付
        ?>
        <div class="payment">
            <div class="payment-top">
                您共有&nbsp;<span><?= $userDoraCoin ?></span>&nbsp;朵拉币可用于支付
            </div>
            <div class="payment-bottom">
                <div class="pay-integral">
                    <div class="pay-left">
                        需要支付的朵拉币
                    </div>
                    <div class="pay-right">
                        <?= $order['COIN'] ?>
                    </div>
                </div>
                <?php
                if ($order['CASH'] > 0) {
                    ?>
                    <div class="pay-money">
                        <div class="pay-money-left">
                            需要支付的现金
                        </div>
                        <div class="pay-money-right">
                            <span><?= $order['CASH'] ?></span>(元)
                        </div>
                    </div>
                    <?php
                }
                ?>
            </div>
        </div>
        <?php
    } else if ($order['STATUS'] == 20 || $order['STATUS'] == 22) {  // 已取消 已失效
        // echo none
    } else {
        ?>
        <div class="payment">
            <div class="payment-bottom">
                <div class="pay-integral">
                    <div class="pay-left">
                        已支付朵拉币
                    </div>
                    <div class="pay-right">
                        <?= $order['COIN'] ?>
                    </div>
                </div>
                <?php
                if ($order['CASH'] > 0) {
                    ?>
                    <div class="pay-money">
                        <div class="pay-money-left">
                            已支付现金
                        </div>
                        <div class="pay-money-right">
                            <span><?= $order['CASH'] ?></span>(元)
                        </div>
                    </div>
                    <?php
                }
                ?>
            </div>
        </div>
        <?php
    }
    ?>

    <?php
    if ($order['STATUS'] == 3 && $virtualCodeList != null) {
        $i = 1;
        echo '<div class="duoLa">';
        foreach ($virtualCodeList as $code) {
            ?>
            <div class="duoLa-juan">
                <div>朵拉劵<?= $i ?>：</div>
                <div>兑换码 <?= $code['CODE'] ?></div>
            </div>

            <?php
            $i++;
        }
        echo '</div>';
    }
    ?>
    <div class="foot-bottom"></div>

    <?php
    switch ($order['STATUS']) {
        case 1: // 未支付
            echo '<div class="order-operator-frame"><a onclick="payConfirm()">订单支付</a><a onclick="orderCancel()">取消订单</a></div>';
            break;
        case 2: // 支付中
            break;
        case 3: // 已支付
            break;
        case 10: // 出库中
            echo '<div class="order-operator-frame"><a href="javascript:searchOrderLogistics()">查看物流</a></div>';
            break;
        case 11: // 配送中
            echo '<div class="order-operator-frame"><a href="javascript:searchOrderLogistics()">查看物流</a></div>';
            break;
        case 12: // 配送完成
            echo '<div class="order-operator-frame"><a href="javascript:searchOrderLogistics()">查看物流</a></div>';
            break;
        case 20: // 已取消
            break;
        case 21: // 已完成
            break;
        case 22: // 已失效
    }
    ?>

</div>

<!--BEGIN toast-->
<div id="toast" style="display: none;">
    <div class="weui_mask_transparent"></div>
    <div class="weui_toast">
        <i class="weui_icon_toast"></i>
        <p class="weui_toast_content">已完成</p>
    </div>
</div>
<!--end toast-->
<!-- loading toast -->
<div id="loading_toast" class="weui_loading_toast" style="display:none;">
    <div class="weui_mask_transparent"></div>
    <div class="weui_toast">
        <div class="weui_loading">
            <div class="weui_loading_leaf weui_loading_leaf_0"></div>
            <div class="weui_loading_leaf weui_loading_leaf_1"></div>
            <div class="weui_loading_leaf weui_loading_leaf_2"></div>
            <div class="weui_loading_leaf weui_loading_leaf_3"></div>
            <div class="weui_loading_leaf weui_loading_leaf_4"></div>
            <div class="weui_loading_leaf weui_loading_leaf_5"></div>
            <div class="weui_loading_leaf weui_loading_leaf_6"></div>
            <div class="weui_loading_leaf weui_loading_leaf_7"></div>
            <div class="weui_loading_leaf weui_loading_leaf_8"></div>
            <div class="weui_loading_leaf weui_loading_leaf_9"></div>
            <div class="weui_loading_leaf weui_loading_leaf_10"></div>
            <div class="weui_loading_leaf weui_loading_leaf_11"></div>
        </div>
        <p class="weui_toast_content">数据加载中</p>
    </div>
</div>
<!-- end loading toast -->

<!--BEGIN dialog confirm-->
<div id="pay_confirm_dialog" class="weui_dialog_confirm" style="display: none;">
    <div class="weui_mask"></div>
    <div class="weui_dialog">
        <div class="weui_dialog_hd"><strong class="weui_dialog_title">支付确认</strong></div>
        <div class="weui_dialog_bd">
            <div class="payment-confirm">
                <div class="payment-confirm-content">
                    <div class="payment-confirm-left">
                        需要支付的朵拉币
                    </div>
                    <div class="payment-confirm-right">
                        <?= $order['COIN'] ?> 朵拉币
                    </div>
                </div>
                <?php
                if ($order['CASH'] > 0) {
                    ?>
                    <div class="payment-confirm-content">
                        <div class="payment-confirm-left">
                            需要支付的现金
                        </div>
                        <div class="payment-confirm-right">
                            <span><?= $order['CASH'] ?></span> (元)
                        </div>
                    </div>
                    <?php
                }
                ?>
            </div>
        </div>
        <div class="weui_dialog_ft">
            <a onclick="payCancel()" href="javascript:;" class="weui_btn_dialog default">取消</a>
            <a onclick="callpay()" id="btnCallPay" href="javascript:;" class="weui_btn_dialog primary">确定</a>
        </div>
    </div>
</div>
<!--END dialog confirm-->

<!--BEGIN dialog confirm-->
<div id="order_cancel_confirm_dialog" class="weui_dialog_confirm" style="display: none;">
    <div class="weui_mask"></div>
    <div class="weui_dialog">
        <div class="weui_dialog_hd"><strong class="weui_dialog_title">是否确认取消此订单?</strong></div>
        <div class="weui_dialog_ft">
            <a onclick="orderCancelCancel()" href="javascript:;" class="weui_btn_dialog default">取消</a>
            <a onclick="orderCancelOk()" href="javascript:;" class="weui_btn_dialog primary">确定</a>
        </div>
    </div>
</div>
<!--END dialog confirm-->
</body>
</html>
